package com.yidianyanglao.buyBack.mapper;

import com.yidianyanglao.buyBack.bean.BankOtherBean;
import com.yidianyanglao.buyBack.bean.BankcardBean;
import com.yidianyanglao.buyBack.bean.RepurchaseRecordBean;
import com.yidianyanglao.user.bean.UserBean;
import com.yidianyanglao.userData.bean.RecommendRecordBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by lf on 2016/11/01.
 */
@Repository
public interface BuyBackMapper {
    /**
     * 解绑银行卡
     * @param userId
     * @param id
     * @return
     */
    @Delete("DELETE FROM yl_bankcard WHERE user_id = #{userId} AND id = #{id}")
    int deleteBankcard(@Param("userId") String userId, @Param("id") String id);

    /**
     * token获取用户
     * @param token
     * @return
     */
    @Select("SELECT * FROM yl_user WHERE token = #{token} and status = 0")
    UserBean getUserByToken(String token);

    /**
     * 我的银行卡列表
     * @param userId
     * @return
     */
    @Select("SELECT * FROM yl_bankcard WHERE user_id = #{userId} order by repurdate desc")
    List<BankcardBean> getBankcardList(String userId);

    /**
     * 支持的银行卡列表
     * @return
     */
    @Select("SELECT * FROM yl_bankother")
    List<BankOtherBean> getBankOtherList();

    /**
     * 通过银行卡号获取银行卡
     * @param cardnumber
     * @param userId
     * @return
     */
    @Select("SELECT * FROM yl_bankcard WHERE cardnumber = #{cardnumber} AND user_id = #{userId}")
    List<BankcardBean>  getBankCardByCardnumber(@Param("cardnumber") String cardnumber,@Param("userId") int userId);

    /**
     * 绑定银行卡
     * @param bankcardBean
     * @return
     */
    @Insert("INSERT INTO yl_bankcard(bank_name,cardnumber,cardusername,idnumber,mobile,user_id,user_name,defaultstate,branch_name) VALUES(#{bankName},#{cardnumber},#{cardusername},#{idnumber},#{mobile},#{userId},#{userName},0,#{branchName})")
    int addBankcard(BankcardBean bankcardBean);

    /**
     * 通过id获取银行卡
     * @param bankId
     * @param userId
     * @return
     */
    @Select("SELECT * FROM yl_bankcard WHERE id = #{bankId} AND user_id = #{userId}")
    BankcardBean getBankCardById(@Param("bankId") String bankId, @Param("userId")  int userId);

    /**
     * 申请回购
     * @param repurchaseRecordBean
     * @return
     */
    @Insert("INSERT INTO yl_repurchase_record(repurchase_id,user_id,user_name,createdate,bank_id,bank_name,card_number,apply_seednum,repurchase_state,taxpoint,taxpoint_value,process_fee,process_fee_value,account_balance,seednum,seed_type,branch_name) VALUES(#{repurchaseId},#{userId},#{userName},#{createdate},#{bankId},#{bankName},#{cardNumber},#{applySeednum},0,#{taxpoint},#{taxpointValue},#{processFee},#{processFeeValue},#{accountBalance},#{seednum},#{seedType},#{branchName})")
    int addRepurchase(RepurchaseRecordBean repurchaseRecordBean);

    /**
     * 更新user普通种子数量
     * @param num
     * @param id
     * @return
     */
    @Update("UPDATE yl_user SET ordinary_seed = #{num},version = version+1 WHERE id = #{id} and version=#{version}")
    int reduceOrdinarySeed(@Param("num") double num, @Param("id") int id,@Param("version")int version);

    /**
     * 更新user奖励种子数量
     * @param num
     * @param id
     * @return
     */
    @Update("UPDATE yl_user SET reward_seed = #{num},version = version+1 WHERE id = #{id} and version = #{version}")
    int reduceRewardSeed(@Param("num") double num, @Param("id") int id,@Param("version")int version);

    /**
     * 获取我的回购记录
     * @param begin
     * @param repurchaseState 状态   0：等待初审 1：审核通过 2：审核不通过 3：等待复审 4：等待财务审核 5：等待财务打款确认'
     * @param userId
     * @return
     */
//    @Select("SELECT * FROM yl_repurchase_record WHERE user_id = #{userId} AND repurchase_state in (#{repurchaseState}) order by createdate desc limit #{begin},5")
    @Select("SELECT * FROM yl_repurchase_record WHERE user_id = #{userId} AND repurchase_state = 1 order by createdate desc limit #{begin},5")
    List<RepurchaseRecordBean> myExpressive(@Param("begin") int begin, @Param("userId") int userId);

    /**
     * 获取我的回购记录
     * @param begin
     * @param repurchaseState1 状态  0：审核中 1：审核通过 2：审核不通过
     * @param repurchaseState2 状态  0：审核中 1：审核通过 2：审核不通过
     * @param userId
     * @return
     */
//    @Select("SELECT * FROM yl_repurchase_record WHERE user_id = #{userId} AND (repurchase_state = #{repurchaseState1} or repurchase_state = #{repurchaseState2}) order by createdate desc limit #{begin},5")
    @Select("SELECT * FROM yl_repurchase_record WHERE user_id = #{userId} AND repurchase_state in (0,2,3,4,5) order by createdate desc limit #{begin},5")
    List<RepurchaseRecordBean> myExpressives(@Param("begin") int begin, @Param("userId") int userId);

    /**
     * 记录银行卡回购时间
     * @param bank
     * @return
     */
    @Update("UPDATE yl_bankcard SET repurdate = #{repurdate} WHERE id = #{id}")
    int setBackRepurdate(BankcardBean bank);

    @Select("SELECT * FROM yl_bankother where bankName=#{bankName} LIMIT 1 ")
    BankOtherBean getBankOtherByName(String bankName);

    @Select("select ifnull(SUM(apply_seednum),0) from yl_repurchase_record where DATE_FORMAT(createdate,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') and user_id=#{id} and repurchase_state != 2")
    double getBankSeedSum(@Param("id")int id);
}
